/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.psa.persistence;

import br.edu.psa.beans.exceptions.NonexistentEntityException;
import br.edu.psa.beans.exceptions.PreexistingEntityException;
import br.edu.psa.beans.exceptions.RollbackFailureException;
import br.edu.psa.model.Administrador;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author JOAO e ANDRIGO
 */
public class AdministradorJpaController extends Controller implements Serializable {

    public AdministradorJpaController(EntityManager em) {
        this.setEntityManager(em);
    }

    public void create(Administrador administrador) throws PreexistingEntityException, RollbackFailureException, Exception {
        try {
            em.persist(administrador);
        } catch (Exception ex) {
            if (findAdministrador(administrador.getId()) != null) {
                throw new PreexistingEntityException("Administrador " + administrador + " already exists.", ex);
            }
            throw ex;
        }
    }

    public void edit(Administrador administrador) throws NonexistentEntityException, RollbackFailureException, Exception {
        try {
            administrador = em.merge(administrador);
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = administrador.getId();
                if (findAdministrador(id) == null) {
                    throw new NonexistentEntityException("The administrador with id " + id + " no longer exists.");
                }
            }
            throw ex;
        }
    }

    public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
        try {
            Administrador administrador;
            try {
                administrador = em.getReference(Administrador.class, id);
                administrador.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The administrador with id " + id + " no longer exists.", enfe);
            }
            em.remove(administrador);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Administrador> findAdministradorEntities() {
        return findAdministradorEntities(true, -1, -1);
    }

    public List<Administrador> findAdministradorEntities(int maxResults, int firstResult) {
        return findAdministradorEntities(false, maxResults, firstResult);
    }

    private List<Administrador> findAdministradorEntities(boolean all, int maxResults, int firstResult) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Administrador.class));
        Query q = em.createQuery(cq);
        if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    }

    public Administrador findAdministrador(Long id) {
        return em.find(Administrador.class, id);
    }

    public int getAdministradorCount() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<Administrador> rt = cq.from(Administrador.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public Administrador findAdministrador(String email, String password) {
        StringBuilder sql = new StringBuilder();
        sql.append("select a from Administrador a where a.email = :email and a.senha = :password");
        Query query = em.createQuery(sql.toString());
        query.setParameter("email", email);
        query.setParameter("password", password);        
        Administrador a = (Administrador) query.getSingleResult();
        return a;
    }
}
